package org.ace.view
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	import flashx.textLayout.operations.MoveChildrenOperation;
	
	import org.ace.communicate.MessageReceived;
	import org.ace.model.MyModel;
	import org.ace.model.SocketModel;
	import org.ace.protocol.LoginProtocol;
	import org.ace.protocol.MapProtocol;
	import org.ace.protocol.Protocol;
	
	public class LoginHandler extends Sprite
	{
		private static var login:LoginHandler;
		private var load:Loader;
		private var loginView:MovieClip;
		public function LoginHandler()
		{
			super();
			
		}
		
		public static function getLogin():LoginHandler{
			if(login==null){
				login=new LoginHandler();
			}
			return login;
		}
		public function process(message:SocketModel):void{
			switch(message.command){
				case LoginProtocol.login_init:
					load=new Loader();
					load.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoad);
					load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loading);
					Loading.getInstance().addLoading();
					Loading.getInstance().setBody("正在加载登录资源");
					load.load(new URLRequest("res/login.swf"));
					break;
				case LoginProtocol.get_role_sres:
					var o:Object=message.message;
					MyModel.id=o.id;
					MyModel.name=o.name;
					MyModel.mapId=o.mapId;
					MyModel.x=o.x;
					MyModel.y=o.y;
					var a:Array=new Array();
					a.push(MyModel.x);
					a.push(MyModel.y);
					MessageReceived.getMessageReceived().write(Protocol.TYPE_MAP,o.mapId,MapProtocol.enter,a);
					break;
			}
		}
		public function loading(e:ProgressEvent):void{
			Loading.getInstance().setIng((e.bytesLoaded/e.bytesTotal*100).toString());
		}
		public function onLoad(e:Event):void{
			load.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,loading);
			load.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoad);
			var myclass:Class =load.contentLoaderInfo.applicationDomain.getDefinition("ace.login") as Class;
			loginView=MovieClip(new myclass);
			loginView.input.login.addEventListener(MouseEvent.CLICK,onClick);
			addChild(loginView);
			MyGameDemo.view.addChild(LoginHandler.getLogin());
			Loading.getInstance().removeLoading();
		}
		public function onClick(e:MouseEvent):void{
			MessageReceived.getMessageReceived().write(Protocol.TYPE_LOGIN,0,LoginProtocol.get_role_creq);
			MyGameDemo.view.removeChild(LoginHandler.getLogin());
		}
	}
}